home *** CD-ROM | disk | FTP | other *** search
/ Adobe Graphics & Publishing SDK 1996 December / Adobe Graphics & Publishing SDK 1996 December.iso / pc / ps40sdk / examples / import / gradientimport / gradientimportuiwin.c < prev   
Encoding:
C/C++ Source or Header  |  1996-09-23  |  15.2 KB  |  566 lines

  1. /*
  2.     File: GradientImportUIWin.c
  3.  
  4.     Copyright 1993-1996, Adobe Systems Incorporated.
  5.     All rights reserved.
  6.  
  7.     C source file for MS-Windows specific code
  8. */
  9.  
  10. #include "GradientImport.h"
  11.  
  12. #define STATE_IDLE        0
  13. #define STATE_AGAIN        1
  14. #define STATE_CANCEL    -1
  15.  
  16. //#define SDK_VALIDATE    WM_USER+100
  17.  
  18. short    state=STATE_IDLE;
  19. short    lastTitem=dRowsItem;
  20. Boolean Validate (GPtr globals, const HWND hDlg, int *item);
  21. void ShowOutputBuffer(GPtr globals, HWND hDlg);
  22.  
  23. /*****************************************************************************/
  24.  
  25. /* Calculates the rectangle we will want to use for the proxy for an image. */
  26.  
  27. void GetProxyItemRect (HWND    hDlg, Rect *proxyItemRect)
  28.     {
  29.  
  30.     RECT    wRect;
  31.     
  32.     GetWindowRect(GetDlgItem(hDlg, kProxyItem), &wRect);    
  33.     ScreenToClient (hDlg, (LPPOINT)&wRect);
  34.     ScreenToClient (hDlg, (LPPOINT)&(wRect.right));
  35.  
  36.     proxyItemRect->left = (short)wRect.left;
  37.     proxyItemRect->top  = (short)wRect.top;
  38.     proxyItemRect->right = (short)wRect.right;
  39.     proxyItemRect->bottom = (short)wRect.bottom;
  40.     
  41.     }
  42.  
  43. /*****************************************************************************/
  44.  
  45. void DoAbout (GPtr globals)
  46. {
  47.     ShowAbout((AboutRecordPtr)gStuff, hDllInstance, AboutID);
  48. }
  49.  
  50. /*****************************************************************************/
  51.  
  52. void ShowOutputBuffer(GPtr globals, HWND hDlg)
  53. {
  54.     PSPixelMap pixels;
  55.     PSPixelMask mask;
  56.     PAINTSTRUCT  ps;
  57.     Rect  itemBounds;
  58.     RECT  wRect;
  59.     POINT    mapOrigin; 
  60.     HDC        hDC;
  61.  
  62.     GetWindowRect(GetDlgItem(hDlg, kProxyItem), &wRect);
  63.     mapOrigin.x = 0;
  64.     mapOrigin.y = 0;
  65.     ClientToScreen(hDlg, &mapOrigin);
  66.  
  67.     /* need to create my own outRect
  68.     itemBounds.left = (short)(((wRect.right + wRect.left - gStuff->outRect.right + gStuff->outRect.left) / 2) - mapOrigin.x);
  69.     itemBounds.top = (short)(((wRect.bottom + wRect.top - gStuff->outRect.bottom + gStuff->outRect.top) / 2) - mapOrigin.y);
  70.     itemBounds.right = (short)(itemBounds.left + (gStuff->outRect.right - gStuff->outRect.left));
  71.     itemBounds.bottom = (short)(itemBounds.top + (gStuff->outRect.bottom - gStuff->outRect.top));
  72.     */ // update this when I can
  73.  
  74.     hDC = BeginPaint(hDlg, &ps);    
  75.  
  76.     wRect.left = itemBounds.left;
  77.     wRect.top = itemBounds.top;
  78.     wRect.right = itemBounds.right;
  79.     wRect.bottom = itemBounds.bottom;
  80.  
  81.     InflateRect(&wRect, kGutter, kGutter);
  82.     //FillRect(hDC, &wRect, (HBRUSH) (COLOR_WINDOW));
  83.     FrameRect(hDC, &wRect, GetStockObject(BLACK_BRUSH));    
  84.     InflateRect(&wRect, -kGutter, -kGutter);
  85.     
  86.     pixels.version = 1;
  87.     /* point these outRect's to your own
  88.     pixels.bounds.top = gStuff->outRect.top;
  89.     pixels.bounds.left = gStuff->outRect.left;
  90.     pixels.bounds.bottom = gStuff->outRect.bottom;
  91.     pixels.bounds.right = gStuff->outRect.right;
  92.     pixels.imageMode = gStuff->imageMode;
  93.     pixels.rowBytes = gStuff->outRowBytes;
  94.     pixels.colBytes = gStuff->outHiPlane - gStuff->outLoPlane + 1;
  95.     pixels.planeBytes = 1;
  96.     pixels.baseAddr = gStuff->outData;
  97.     */ // need to be updated
  98.  
  99.     pixels.mat = NULL;
  100.     pixels.masks = NULL;
  101.     pixels.maskPhaseRow = 0;
  102.     pixels.maskPhaseCol = 0;
  103.  
  104.     /* do something with this mask, layer, and floating stuff */
  105. #if 0
  106.     if (gStuff->isFloating) {
  107.         mask.next = NULL;
  108.         mask.maskData = gStuff->maskData;
  109.         mask.rowBytes = gStuff->maskRowBytes;
  110.         mask.colBytes = 1;
  111.         mask.maskDescription = kSimplePSMask;
  112.     
  113.         pixels.masks = &mask;
  114.     } else if ((gStuff->inLayerPlanes != 0) && (gStuff->inTransparencyMask != 0)) {
  115.         mask.maskData = ((char *) gStuff->outData) + (gStuff->outHiPlane - gStuff->outLoPlane);
  116.         mask.rowBytes = gStuff->outRowBytes;
  117.         mask.colBytes = gStuff->outHiPlane - gStuff->outLoPlane + 1;
  118.         mask.next = NULL;
  119.         mask.maskDescription = kSimplePSMask;
  120.     
  121.         pixels.masks = &mask;
  122.     }
  123.         // fix the above
  124. #endif
  125.     mask.next = NULL; // placeholder
  126.  
  127.     (gStuff->displayPixels)(&pixels, &pixels.bounds, itemBounds.top, itemBounds.left, (void *)hDC);
  128.  
  129.     EndPaint(hDlg, (LPPAINTSTRUCT) &ps);
  130. }
  131.  
  132. /*****************************************************************************/
  133. // Because we want to wait until the user changes the focus
  134. // from an EditText field to something else, and because we
  135. // want to ignore validation if the user Cancels, this routine
  136. // does round of validating fields if it's been flagged.
  137.  
  138. Boolean Validate (GPtr globals, const HWND hDlg, int *item)
  139. {
  140.     int32    x = 0;
  141.     Boolean retn = true;
  142.     short    numberErr = noErr;
  143.  
  144.     switch (*item)
  145.     {
  146.         case dColsItem:
  147.             numberErr = FetchNumber(hDlg, 
  148.                                dColsItem, 
  149.                                kColumnsMin, 
  150.                                kColumnsMax, 
  151.                                &x);
  152.             if (numberErr != noErr)
  153.             { // shows alert if there's an error
  154.                 AlertNumber(hDlg,
  155.                             dColsItem,
  156.                             kColumnsMin,
  157.                             kColumnsMax,
  158.                             &x,
  159.                             hDllInstance,
  160.                             AlertID,
  161.                             numberErr);
  162.                 retn = false; // had to pop an error, stay here
  163.             }
  164.             break;
  165.         case dRowsItem:
  166.             numberErr = FetchNumber(hDlg, 
  167.                                dRowsItem, 
  168.                                kRowsMin, 
  169.                                kRowsMax, 
  170.                                &x);
  171.             if (numberErr != noErr)
  172.             { // shows alert if there's an error
  173.                 AlertNumber(hDlg,
  174.                             dRowsItem,
  175.                             kRowsMin,
  176.                             kRowsMax,
  177.                             &x,
  178.                             hDllInstance,
  179.                             AlertID,
  180.                             numberErr);
  181.                 retn = false; // had to pop an error, stay here
  182.             }
  183.             break;
  184.     }
  185.     *item = 0;
  186.     return retn;
  187. }
  188.  
  189. /*****************************************************************************/
  190.  
  191. /* Dialog box code to ask for the acquire parameters. */
  192.  
  193. BOOL WINAPI UIProc(HWND hDlg, UINT wMsg, WPARAM wParam, LPARAM lParam)     // Win32 Change
  194. {
  195.     static GPtr globals=NULL;          /* need to be static    */
  196.  
  197.     int32 x = 0;
  198.     static int lastItem = 0;
  199.     int item;
  200.     int cmd;
  201.     short numberErr = noErr;
  202.     static HWND h = NULL; // last window with focus
  203.     static PlatformData *platform;
  204.     
  205.     switch  (wMsg)
  206.     {
  207.       case WM_INITDIALOG:
  208.         /* set up globals    */
  209.         globals = (GPtr) lParam;
  210.         platform = ((AcquireRecordPtr) gStuff)->platformData;
  211.             
  212.         CenterDialog(hDlg);
  213.                 
  214.         // Initialize Images, width and height edit boxes with default values
  215.         StuffNumber(hDlg, dRowsItem, gLastRows);
  216.         StuffNumber(hDlg, dColsItem, gLastCols);
  217.         SetCheckBoxState (hDlg, dInvertItem, (gLastInvert && gStuff->canReadBack));
  218.  
  219.         /* We will only be inverting if we can read the image back. */
  220.  
  221.         if (gStuff->canReadBack)
  222.             EnableControl (hDlg, dInvertItem);
  223.         else
  224.             DisableControl (hDlg, dInvertItem);
  225.  
  226.         // Initialize mode to its default
  227.         SetRadioGroupState(hDlg, 
  228.                            dFirstModeItem, 
  229.                            dLastModeItem, 
  230.                            gLastMode - plugInModeBitmap + dFirstModeItem);
  231.     
  232.         // Rows edit box will get the focus
  233.         SelectTextItem(hDlg, dRowsItem);
  234.         return FALSE;
  235.         break;
  236.       case WM_PAINT:
  237.         return FALSE; // handled it
  238.         break;
  239.       case WM_ACTIVATE: 
  240.         if (LOWORD(wParam) == WA_INACTIVE && 
  241.             state == STATE_IDLE && 
  242.             lParam == (LPARAM)platform->hwnd)
  243.         {
  244.              /* Bug in non-modal lets parent get clicked.
  245.                 This will post the reversing deactivate and activate
  246.                 messages to fix it. */
  247.             h = GetFocus();
  248.             PostMessage((HWND)lParam, WM_ACTIVATE, (WPARAM)WA_INACTIVE,
  249.                         (LPARAM)hDlg); // inactivate host
  250.             PostMessage((HWND)hDlg, WM_ACTIVATE, (WPARAM)WA_ACTIVE,
  251.                         lParam); // activate us again
  252.             MessageBeep(MB_OK); 
  253.         }
  254.         else if (h != NULL) // reset the focus on activate
  255.         {
  256.             SetDialogDefaultItem(hDlg, ok); // outline
  257.             SetFocus(h);
  258.             h = NULL;
  259.         }
  260.         return FALSE; // 0=we handled it
  261.         break;
  262.       case WM_COMMAND:
  263.         item = COMMANDID (wParam);              // WIN32 Change
  264.         cmd = HIWORD(wParam);
  265.  
  266.         switch (item)
  267.         {
  268.             case ok:
  269.                 if (cmd == BN_CLICKED)
  270.                 {
  271.                     lastItem = dRowsItem; // force validation
  272.                     if (Validate(globals, hDlg, &lastItem))
  273.                     {
  274.                         numberErr = FetchNumber(hDlg, 
  275.                                 dRowsItem, 
  276.                                 kRowsMin, 
  277.                                 kRowsMax, 
  278.                                 &x);
  279.                         
  280.                         lastItem = dColsItem; 
  281.                         if (Validate(globals, hDlg, &lastItem))        
  282.                         {
  283.                             gLastRows = (short)x;
  284.                         
  285.                             numberErr = FetchNumber(hDlg, 
  286.                                 dColsItem, 
  287.                                 kColumnsMin, 
  288.                                 kColumnsMax, 
  289.                                 &x);
  290.                             gLastCols = (short)x;
  291.  
  292.                             gLastMode = GetRadioGroupState(hDlg,
  293.                                                    dFirstModeItem,
  294.                                                    dLastModeItem)
  295.                                                    - dFirstModeItem
  296.                                                    + plugInModeBitmap;
  297.                             gLastInvert = GetCheckBoxState(hDlg, dInvertItem);
  298.                             state = STATE_AGAIN;
  299.                         } // bad cols
  300.                     } // bad rows
  301.                 } // btn clicked
  302.                 else Validate(globals, hDlg, &lastItem);
  303.                 // btn not clicked but focus may have changed to it so validate
  304.                 break;
  305.  
  306.             case cancel:
  307.                 if (cmd == BN_CLICKED)
  308.                     state = STATE_CANCEL;
  309.                 // don't validate ever when we're dealing with cancel
  310.                 break;
  311.  
  312.             case dFirstModeItem:
  313.             case dFirstModeItem+1:
  314.             case dFirstModeItem+2:
  315.             case dFirstModeItem+3:
  316.                 if (Validate(globals, hDlg, &lastItem))
  317.                     if (cmd == BN_CLICKED)
  318.                         SetRadioGroupState(hDlg, dFirstModeItem, dLastModeItem, wParam);
  319.                 break;
  320.             case dInvertItem:
  321.                 if (Validate(globals, hDlg, &lastItem))
  322.                     if (cmd == BN_CLICKED)
  323.                         ToggleCheckBoxState(hDlg, dInvertItem);
  324.                 break;
  325.             case dRowsItem:
  326.                 if (Validate(globals, hDlg, &lastItem))
  327.                 {
  328.                     if (cmd == EN_KILLFOCUS)
  329.                         lastTitem = lastItem = dRowsItem; // for validation
  330.                     else if (cmd == EN_CHANGE)
  331.                     {
  332.                         numberErr = FetchNumber(hDlg,
  333.                                             dRowsItem,
  334.                                             kRowsMin,
  335.                                             kRowsMax,
  336.                                             &x);
  337.                         if (numberErr == noErr && x != gLastRows)
  338.                         { // different number, update
  339.                             /* later implementation
  340.                                 gLastRows = (short)x;
  341.  
  342.                                 // Do Filtering operation
  343.                                 DoFilterRect (globals);
  344.  
  345.                                 // Invalidate Proxy Item
  346.                                 GetWindowRect(GetDlgItem(hDlg, kProxyItem), &imageRect);
  347.                                 ScreenToClient (hDlg, (LPPOINT)&imageRect);
  348.                                 ScreenToClient (hDlg, (LPPOINT)&(imageRect.right));
  349.                                 InvalidateRect (hDlg, &imageRect, FALSE);
  350.                             */ // for later
  351.                         }
  352.                     }
  353.                 }
  354.                 break;
  355.             case dColsItem:
  356.                 if (Validate(globals, hDlg, &lastItem))
  357.                 {
  358.                     if (cmd == EN_KILLFOCUS)
  359.                         lastTitem = lastItem = dColsItem; // for validate routine
  360.                     else if (cmd == EN_CHANGE)
  361.                     {
  362.                         numberErr = FetchNumber(hDlg,
  363.                                             dColsItem,
  364.                                             kColumnsMin,
  365.                                             kColumnsMax,
  366.                                             &x);
  367.                         if (numberErr == noErr && x != gLastCols)
  368.                         { // different number, update
  369.                             /* later implementation
  370.                                 gLastCols = (short)x;
  371.  
  372.                                 // Do Filtering operation
  373.                                 DoFilterRect (globals);
  374.  
  375.                                 // Invalidate Proxy Item
  376.                                 GetWindowRect(GetDlgItem(hDlg, kProxyItem), &imageRect);
  377.                                 ScreenToClient (hDlg, (LPPOINT)&imageRect);
  378.                                 ScreenToClient (hDlg, (LPPOINT)&(imageRect.right));
  379.                                 InvalidateRect (hDlg, &imageRect, FALSE);
  380.                             */ // for later
  381.                         }
  382.                     }
  383.                 }
  384.                 break;
  385.         } /* switch (item) */
  386.         return FALSE;
  387.         break;
  388.  
  389.       default:
  390.         return FALSE; // we didn't handle it FALSE;
  391.         break;
  392.     } /* switch */
  393.     return TRUE; // we didn't handle it FALSE;
  394. }
  395.  
  396. /*****************************************************************************/
  397.  
  398. /* Setup the parameters dialog.  Returns TRUE if it succeeds.                 */
  399.  
  400. Boolean OpenOurDialog (GPtr globals)
  401.     {
  402.     
  403.     PlatformData *platform;
  404.  
  405.     state = STATE_IDLE;
  406.     
  407.     if (gDialog != NULL)
  408.     {
  409.         SetActiveWindow (gDialog);
  410.         // Select first text item again
  411.         if (lastTitem > 0)
  412.         { // reselect last text item
  413.             SelectTextItem(gDialog, lastTitem);
  414.             lastTitem = 0;
  415.         }
  416.         // set the dialog window the active window
  417.         return TRUE;
  418.     }
  419.     else
  420.     { // dialog not created yet.  Do it.
  421.         platform = ((AcquireRecordPtr) gStuff)->platformData;
  422.  
  423.  
  424.         gDialog = CreateDialogParam(hDllInstance,
  425.                                     (LPSTR)"UIPARAM",
  426.                                     (HWND)platform->hwnd,
  427.                                     (DLGPROC)UIProc, // FARPROC
  428.                                     (LPARAM)globals);
  429.     
  430.         if (gDialog)
  431.         { // got it.  Show it.
  432.             ShowWindow(gDialog, SW_NORMAL);
  433.             UpdateWindow(gDialog);
  434.             return TRUE;
  435.         }
  436.         else // lost it.
  437.             return FALSE;        // Couldnt put up the dialog box        
  438.     }
  439. }
  440.  
  441. /*****************************************************************************/
  442.  
  443. /* Run the parameters dialog.  Returns TRUE if it succeeds.                     */
  444.  
  445. Boolean RunOurDialog (GPtr globals)
  446.     {
  447.     // We are basically waiting for user to press "Import" or "Cancel" from the 
  448.     // dialog
  449.  
  450.     //HWND pWin = NULL; // parent window
  451.     MSG msg;
  452.     
  453.     // First post a message to the host to update itself
  454.     
  455.     /*
  456.     pWin = GetParent(gDialog); // gets the parent (host)
  457.     RedrawWindow(pWin, NULL, NULL, RDW_INTERNALPAINT | RDW_UPDATENOW | RDW_ALLCHILDREN);        
  458.     */
  459.  
  460.     while (GetMessage((LPMSG)&msg, NULL, 0, 0))
  461.     {
  462.         if (!IsDialogMessage((HWND)gDialog, &msg))
  463.         { // wasn't for our dialog.  Send it on its way.
  464.             TranslateMessage((LPMSG)&msg);
  465.             DispatchMessage ((LPMSG)&msg);
  466.         }
  467.         
  468.  
  469.         // state is set in UIProc based on the user response
  470.         // The only way this function would return is by the user responding with
  471.         // either pressing the "Import" or the "Cancel" button
  472.         if (state == STATE_AGAIN)
  473.         { 
  474.             return TRUE;
  475.         }
  476.         else if (state == STATE_CANCEL)
  477.         {
  478.             return FALSE;
  479.         }
  480.     }
  481. }
  482.  
  483.  
  484. /*****************************************************************************/
  485.  
  486. /* Close the parameters dialog.                                                  */
  487.  
  488. void CloseOurDialog (GPtr globals)
  489. {
  490.     
  491.     if (gDialog != NULL)
  492.     {
  493.         DestroyWindow(gDialog);
  494.         gDialog = NULL;
  495.     }
  496.     
  497. }
  498.  
  499. /****************************************************************************/
  500. /* Example for ShowAlert() function which takes a string ID as parameter    */
  501. /* and displays a message box                                               */
  502. /****************************************************************************/
  503.  
  504. short ShowAlert (short stringID)
  505. {
  506.     char szMessage[256];
  507.     char szTitle[128];
  508.  
  509.     LoadString(hDllInstance, stringID, szMessage, sizeof szMessage);
  510.     LoadString(hDllInstance, 2, szTitle, sizeof szTitle);
  511.     return  ( MessageBox(NULL, szMessage, szTitle, MB_OK | MB_ICONHAND) );
  512.  
  513. }
  514.  
  515. /* Initialization and termination code for window's dlls. */
  516.  
  517. // Win32 Change
  518. #ifdef WIN32
  519.  
  520. // Every 32-Bit DLL has an entry point DLLInit
  521.  
  522. BOOL APIENTRY DLLInit(HANDLE hInstance, DWORD fdwReason, LPVOID lpReserved)
  523. {
  524.  
  525.     if (fdwReason == DLL_PROCESS_ATTACH)
  526.         hDllInstance = hInstance;
  527.  
  528.     return TRUE;   // Indicate that the DLL was initialized successfully.
  529. }
  530.  
  531. #else
  532. /* ------------------------------------------------
  533.  *   Code from Borland's window's dll example code.
  534.  * ------------------------------------------------
  535.  */
  536. #if defined(__BORLANDC__)
  537. // Turn off warning: Parameter '' is never used; effects next function only
  538. #pragma argsused
  539. #endif
  540.  
  541. // Every DLL has an entry point LibMain and an exit point WEP.
  542. int FAR PASCAL LibMain( HANDLE hInstance, WORD wDataSegment,
  543.                                    WORD wHeapSize, LPSTR lpszCmdLine )
  544. {
  545.     // Required when using Zortech; causes blink to include startup code
  546.     extern __acrtused_dll;
  547.  
  548.     // The startup code for the DLL initializes the local heap (if there is one)
  549.     // with a call to LocalInit which locks the data segment.
  550.     if ( wHeapSize != 0 )
  551.         UnlockData( 0 );
  552.  
  553.     hDllInstance = hInstance;
  554.     return 1;   // Indicate that the DLL was initialized successfully.
  555. }
  556.  
  557. int FAR PASCAL WEP(int nParam)
  558. {
  559.     switch  (nParam) {
  560.       case  WEP_SYSTEM_EXIT: // System shutdown in progress
  561.       case  WEP_FREE_DLL   : // DLL use count is 0
  562.       default :              // Undefined;  ignore
  563.             return  1;
  564.     }
  565. }
  566. #endif